package com.banmao.mall.service.client.impl;

import com.banmao.mall.mapper.TopicMapper;
import com.banmao.mall.model.pojo.Topic;
import com.banmao.mall.model.vo.client.topic.TopicDetailVO;
import com.banmao.mall.model.vo.client.topic.TopicListClientVO;
import com.banmao.mall.service.client.TopicService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TopicServiceImpl implements TopicService {

    @Autowired
    TopicMapper topicMapper;

    @Override
    public TopicListClientVO list(int page, int size) {
        //分页
        PageHelper.startPage(page,size);
        Topic topic = new Topic();
        topic.setDeleted(false);
        List<Topic> topicList = topicMapper.selectNoDeleted(topic);
        PageInfo<Topic> topicPageInfo = new PageInfo<>(topicList);
        long total = topicPageInfo.getTotal();
        TopicListClientVO topicListClientVO = new TopicListClientVO();
        topicListClientVO.setCount((int) total);
        topicListClientVO.setData(topicList);
        return topicListClientVO;
    }

    @Override
    public TopicDetailVO detail(int id) {
        /**
         * 1.因为表格设计不合理，专题当中的goods无法被添加，所以恒为空数组。
         * 2.因为显示出来的专题必定没有删除，所以查询时不用判断是否删除。
         */
        Topic topic = topicMapper.selectByPrimaryKey(id);
        String[] goods = topic.getGoods();
        TopicDetailVO topicDetailVO = new TopicDetailVO();
        topicDetailVO.setTopic(topic);
        topicDetailVO.setGoods(goods);
        return topicDetailVO;
    }

    @Override
    public List<Topic> related(int id) {
        /**
         * 推荐数量设为4个
         */
        List<Topic> topicList = topicMapper.selectSelective(id,4);
        return topicList;
    }
}
